package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Dept;
import com.wn55.hrms.service.DeptService;
import com.wn55.hrms.service.vo.DeptVo;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @author 曹金鑫~~~
 * @date 2022/04/29/0:19
 */
@Api(value = "此接口用于操作部门管理功能")
@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    DeptService deptService;

    /**
     * 模糊查询所有部门
     *
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/listAllDept")
    @ApiOperation(value = "分页查询部门的列表")
    @Cacheable(value = "deptPagesData",keyGenerator = "keyGenerator")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "currentPage", value = "查询当前页", required = false, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "每页显示多少条", required = false, defaultValue = "10"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "部门名称", required = false)
    })
    public ResultResponse<PageInfo<DeptVo>> listAllDept(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      String name) {
        ResultResponse<PageInfo<DeptVo>> result = null;
        try {
            PageInfo<DeptVo> pageInfo = deptService.selectDeptByCondition(currentPage, pageSize, name);
            result = new ResultResponse<>(200, "查询部门列表成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询部门列表失败");
        }
        return result;
    }

    @GetMapping("/initDept")
    public ResultResponse<List<Dept>> initDept(){
        ResultResponse<List<Dept>> result = null;
        try {
            List<Dept> depts = deptService.initDept();
            result = new ResultResponse<>(200,"获取成功",depts);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"获取失败");
        }
        return result;
    }

    /**
     * 添加部门
     *
     * @param dept
     * @return
     */
    @PostMapping("/addDept")
    public ResultResponse<Void> addDept(@RequestBody Dept dept) {
        ResultResponse<Void> result = null;
        try {
            dept.setComeTime(new Date());
            dept.setCreateTime(new Date());
            dept.setUpdateTime(new Date());
            dept.setIsdelete(0);
            deptService.addDept(dept);
            result = new ResultResponse<>(200, "添加部门成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加部门失败");
        }
        return result;
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @PutMapping("/editDept")
    public ResultResponse<Void> editDept(@RequestBody Dept dept) {
        ResultResponse<Void> result = null;
        try {
            dept.setUpdateTime(new Date());
            deptService.editDept(dept);
            result = new ResultResponse<>(200, "修改部门成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "更新部门失败");
        }
        return result;
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @PutMapping("/delete/{id}")
    public ResultResponse<Void> delete(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            deptService.deleteDeptById(id);
            result = new ResultResponse<>(200, "删除部门成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除部门失败");
        }
        return result;
    }


}
